package org.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.example.model.common.AppHttpCodeEnum;
import org.example.model.common.ResponseResult;
import org.example.model.image.ImageHistory;
import org.example.model.image.ImageResult;
import org.example.model.image.dto.ImageResp;
import org.example.model.image.dto.ImageTraditionReq;
import org.example.service.image.IImageHistoryService;
import org.example.service.image.IImageResultService;
import org.example.utils.HttpClientUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 图片历史记录 前端控制器
 * </p>
 *
 * @author yjx
 * @since 2025-04-04
 */
@RestController
@RequestMapping("/imageHistory")
@Api(value = "图片处理接口", tags = {"图片处理接口"})
public class ImageHistoryController {

    @Autowired
    private IImageHistoryService imageHistoryService;

    @Autowired
    private IImageResultService iImageResultService;

    /**
     * 传统学习处理
     */
    @PostMapping("/traditional")
    @ApiOperation("传统学习处理")
    public ResponseResult<ImageTraditionReq> AddUserInfo(@RequestBody ImageTraditionReq imageTraditionReq) {
        Integer order = imageTraditionReq.getHandleOrder();
        ResponseResult<ImageTraditionReq> result = new ResponseResult<>();
        ImageHistory imageHistory = new ImageHistory();
        // 相同属性数据拷贝
        BeanUtils.copyProperties(imageTraditionReq, imageHistory);
        // 设置类型图片处理类型
        imageHistory.setType(0);
        String currentResult = HttpClientUtil.doPostTradition(imageTraditionReq.getHandleOrder(), imageTraditionReq.getFilePath());
        ImageResult imageResult = new ImageResult();
        imageResult.setHandleOrder(order);
        ImageTraditionReq traditionResp = new ImageTraditionReq();
        BeanUtils.copyProperties(imageTraditionReq, traditionResp);
        if (imageTraditionReq.getId() == 0) {
            imageHistoryService.save(imageHistory);
            imageResult.setImageId(imageHistory.getId());
            traditionResp.setId(imageHistory.getId());
        } else {
            imageResult.setImageId(imageTraditionReq.getId());
        }
        traditionResp.setResult(currentResult);
        imageResult.setResult(currentResult);
        iImageResultService.save(imageResult);
        result.ok(AppHttpCodeEnum.SUCCESS.getCode(), traditionResp, AppHttpCodeEnum.SUCCESS.getErrorMessage());
        return result;
    }

    /**
     * 深度学习处理
     */
    @PostMapping("/deep")
    @ApiOperation("深度学习处理")
    public ResponseResult<ImageResp> AddUserInfo(String filePath) {
        ResponseResult<ImageResp> result = new ResponseResult<>();
        ImageHistory imageHistory = new ImageHistory();
        imageHistory.setFilePath(filePath);
        imageHistory.setType(1);
        boolean flag = imageHistoryService.save(imageHistory);
        List<ImageResult> imageResultList = new ArrayList<>();
        ImageResult imageResult = null;
        if (flag) {
            imageResult = new ImageResult();
            imageResult.setImageId(imageHistory.getId());
            imageResult.setHandleOrder(0);
            String currentResult = HttpClientUtil.doPostDeep(filePath);
            imageResult.setResult(currentResult);
            flag =  iImageResultService.save(imageResult);
        }
        if (flag) {
            imageResultList.add(imageResult);
            ImageResp imageResp = new ImageResp();
            BeanUtils.copyProperties(imageHistory, imageResp);
            imageResp.setImageResultList(imageResultList);
            result.ok(AppHttpCodeEnum.SUCCESS.getCode(), imageResp, AppHttpCodeEnum.SUCCESS.getErrorMessage());
            return result;
        }
        result.error(AppHttpCodeEnum.SERVER_ERROR.getCode(), AppHttpCodeEnum.SERVER_ERROR.getErrorMessage());
        return result;
    }

    /**
     * 历史记录
     */
    @GetMapping("/history")
    @ApiOperation("历史记录")
    public ResponseResult<List<ImageResp>> getImageHistory() {
        ResponseResult<List<ImageResp>> result = new ResponseResult<>();
        List<ImageHistory> historyList = imageHistoryService.list();
        List<ImageResp> imageRespList = new ArrayList<>();
        ImageResp imageResp = null;
        for (ImageHistory imageHistory : historyList) {
            imageResp = new ImageResp();
            BeanUtils.copyProperties(imageHistory, imageResp);
            LambdaQueryWrapper<ImageResult> resQuery = new LambdaQueryWrapper<>();
            resQuery.eq(ImageResult::getImageId, imageHistory.getId());
            List<ImageResult> imageResultList = iImageResultService.list(resQuery);
            imageResp.setImageResultList(imageResultList);
            imageRespList.add(imageResp);
        }
        result.ok(AppHttpCodeEnum.SUCCESS.getCode(), imageRespList, AppHttpCodeEnum.SUCCESS.getErrorMessage());
        return result;
    }

    /**
     * 删除历史记录
     */
    @DeleteMapping("/history")
    @ApiOperation("删除历史记录")
    public ResponseResult<Boolean> delImageHistory(Integer id) {
        ResponseResult<Boolean> result = new ResponseResult<>();
        boolean flag = imageHistoryService.removeById(id);
        if (flag) {
            LambdaQueryWrapper<ImageResult> resQuery = new LambdaQueryWrapper<>();
            resQuery.eq(ImageResult::getImageId, id);
            flag = iImageResultService.remove(resQuery);
        }
        result.ok(AppHttpCodeEnum.SUCCESS.getCode(), flag, AppHttpCodeEnum.SUCCESS.getErrorMessage());
        return result;
    }

}
